package com.hice.demo.sbdbra;

import com.hice.demo.sbdbra.common.dto.ErrorInfo;
import com.hice.demo.sbdbra.exception.SbdbraException;
import org.beetl.sql.core.BeetlSQLException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 统一异常处理
 * @author hyson
 * @create 2017-02-23 15:54
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 统一处理beetlsql异常
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = BeetlSQLException.class)
    @ResponseBody
    public ErrorInfo<String> beetlSQLException(HttpServletRequest req, BeetlSQLException e) throws Exception {
        ErrorInfo<String> r = new ErrorInfo<>();
        r.setMessage(e.getMessage());
        r.setCode(1000 + e.getCode());
        r.setData("Some Data");
        r.setUrl(req.getRequestURL().toString());
        return r;
    }

    /**
     * 统一处理frs模块异常
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = SbdbraException.class)
    @ResponseBody
    public ErrorInfo<String> frsException(HttpServletRequest req, SbdbraException e) throws Exception {
        ErrorInfo<String> r = new ErrorInfo<>();
        r.setMessage(e.getMessage());
        r.setCode(2000 + e.getCode());
        return r;
    }

}
